/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function trimFieldValue(field){
    if(field !== undefined && field !== null)
        return field.value = trim(field.value);
    return null;
}

function trimL( value ){
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}
// Removes ending whitespaces
function trimR( value ){
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ){
    return trimL(trimR(value));
}
        
function emptyField(str){
    return str.value.trim() === "" ? true : false;
}

function showMessage(str,objField){
    alert(str);
    if(objField !== undefined){
       objField.focus();
    }
}

function confirmMessage(msg){
    return confirm(msg);
}

function isEmail(email) { 
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        return false;
    }
    return true;
} 

function isPhone(phone){
    var filter =/^\d{10}$/;
    if (!filter.test(phone)) {
        return false;
    }
    return true;
}

var Ajax={
    applicationUrl:'',
    authorizeUrl:'',
    callFunction:function(url, agrs)
    {
        var ret = '';
        var params ='';
        for(var key in agrs){
            params +=  "&"+encodeURIComponent(key)+"="+encodeURIComponent(agrs[key]);
        }
        
        jQuery.ajax({
            type: "POST",
            url: Ajax.applicationUrl+url + "?_IS_AJAX=1",
            data: params,
            cache: false,
            async: false,
            success: function(result) {
                ret = result;
            },
            error: function(transport) {
                alert("ERROR");
            }
        });
        if (ret === undefined || ret === null) {
            location.href = Ajax.authorizeUrl;
            setTimeout(1000);
            return '';
        }
        else {
            return JSON.parse(ret);
        }
    },
      
    callAsyncFunction:function(url, agrs,callback)
    {
        var params ='';
        if(agrs !== null){
            for(var key in agrs){
                params +=  "&"+encodeURIComponent(key)+"="+encodeURIComponent(agrs[key]);
            }
        }
        jQuery.ajax({
            type: "POST",
            url: Ajax.applicationUrl + url + "?_IS_AJAX=1",
            data: params,
            cache: false,
            async: true,
            success: function(result) {
                if (callback && typeof(callback) === "function") {
                    callback(JSON.parse(result));
                }
            },
            error: function(transport) {
                alert("ERROR");
            }
        });
        
    }
};


function addOption(el,label, value, selected) {
    opt = document.createElement("option");
    opt.appendChild(document.createTextNode(label));
    opt.value  = value;
    opt.selected = selected;
    el.appendChild(opt);
}

function removeOption(idOption,from_index){
    if(from_index === undefined){
        from_index = 0;
    }
    var options = document.getElementById(idOption);
    for(var i=options.length-1;i>=from_index;i--){
        options.remove(i); 
    }
}

function getBodyWidth(){
    if (typeof(window.innerWidth) === 'number') {
        //Non-IE
        bodyWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        bodyWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        bodyWidth = document.body.clientWidth;
    }
    return bodyWidth;
}

function getBodyHeight(){
    if (typeof(window.innerWidth) === 'number') {
        //Non-IE
        bodyHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        bodyHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        bodyHeight = document.body.clientHeight;
    }
    return bodyHeight;
}
				
function  getWidth(idPopup){
    var popUp=document.getElementById(idPopup);
    return popUp.offsetWidth;
}
					
function  getHeight(idPopup){
    var popUp=document.getElementById(idPopup);
    return popUp.offsetHeight;
}
					

function isValidLengthTextBox(control, error_message, strTitle, strLength)
{
    if (control.value === null || control.value.length > strLength)
    {
        error_message = error_message.replace('[#NAME#]', strTitle);
        showMessage(error_message.replace('[#MAX_LENGTH#]', strLength),control);
        return false;
    }
    return true;
}


var LoadingIcon = {
    width:100,
    height:100,
    position:"fixed",
    isProcessing:false,
    popupClassName:"loading",
    popupObj:document.createElement('div'),
    show:function(){
       var bodyWidth = getBodyWidth();
       var bodyHeight = getBodyHeight();
       LoadingIcon.popupObj.style.width = LoadingIcon.width+"px";
       LoadingIcon.popupObj.style.height = LoadingIcon.height+"px";
       LoadingIcon.popupObj.className = LoadingIcon.popupClassName;
       LoadingIcon.popupObj.style.position = LoadingIcon.position;
       LoadingIcon.popupObj.style.left=((bodyWidth-LoadingIcon.width)/2)+"px";
       
       var topScroll = window.pageYOffset || document.documentElement.scrollTop;
       if(LoadingIcon.position === "fixed"){
            LoadingIcon.popupObj.style.top=  ((bodyHeight-LoadingIcon.height)/2) +"px";
       }else{
            LoadingIcon.popupObj.style.top= ( topScroll +  ((bodyHeight-LoadingIcon.height)/2) )+"px";
       }
       LoadingIcon.popupObj.style.display = "";
       document.body.appendChild(LoadingIcon.popupObj);
    },
    
    hide:function(){
       LoadingIcon.popupObj.style.display="none";
    }
};